/*
*  Copyright 2019-2020 gaosc
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.admin.qujiang.service.impl;

import com.admin.qujiang.model.YlDatakbGovernmentServiceIntegrationModel;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.admin.qujiang.service.IYlDatakbGovernmentServiceIntegrationService;
import com.admin.qujiang.dto.YlDatakbGovernmentServiceIntegrationDto;
import com.admin.qujiang.service.dto.YlDatakbGovernmentServiceIntegrationQueryCriteria;
import com.admin.qujiang.mapper.YlDatakbGovernmentServiceIntegrationMapper;
import com.admin.utils.DozerUtils;
import org.dozer.Mapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author gaosc
* @date 2022-05-31
**/
@Service
@RequiredArgsConstructor
public class YlDatakbGovernmentServiceIntegrationServiceImpl extends ServiceImpl<YlDatakbGovernmentServiceIntegrationMapper, YlDatakbGovernmentServiceIntegrationModel> implements IYlDatakbGovernmentServiceIntegrationService {

    // dozer做Model与DTO的转换
    private final Mapper mapper;
    
    private QueryWrapper<YlDatakbGovernmentServiceIntegrationModel> buildWrapper(YlDatakbGovernmentServiceIntegrationQueryCriteria criteria, Boolean isQuery) {
        QueryWrapper<YlDatakbGovernmentServiceIntegrationModel> query = null;
        if (null != criteria) {
            query = new QueryWrapper<YlDatakbGovernmentServiceIntegrationModel>();
        }
        return query;
    }

    @Override
    public List<YlDatakbGovernmentServiceIntegrationDto> queryAll(YlDatakbGovernmentServiceIntegrationQueryCriteria criteria, Boolean isQuery){
        List<YlDatakbGovernmentServiceIntegrationModel> ylDatakbGovernmentServiceIntegrations = this.list(buildWrapper(criteria, isQuery));
        return DozerUtils.mapList(mapper, ylDatakbGovernmentServiceIntegrations, YlDatakbGovernmentServiceIntegrationDto.class);
    }

    @Override
    @Transactional
    public YlDatakbGovernmentServiceIntegrationDto findById(Long id) {
        YlDatakbGovernmentServiceIntegrationModel ylDatakbGovernmentServiceIntegration = Optional.ofNullable(this.getById(id)).orElseGet(YlDatakbGovernmentServiceIntegrationModel::new);
        ValidationUtil.isNull(ylDatakbGovernmentServiceIntegration.getId(),"YlDatakbGovernmentServiceIntegration","id",id);
        return mapper.map(ylDatakbGovernmentServiceIntegration, YlDatakbGovernmentServiceIntegrationDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YlDatakbGovernmentServiceIntegrationModel create(YlDatakbGovernmentServiceIntegrationModel resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        this.save(resources);
        return resources;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YlDatakbGovernmentServiceIntegrationModel resources) {
        YlDatakbGovernmentServiceIntegrationModel ylDatakbGovernmentServiceIntegration = Optional.ofNullable(this.getById(resources.getId())).orElseGet(YlDatakbGovernmentServiceIntegrationModel::new);
        ValidationUtil.isNull(ylDatakbGovernmentServiceIntegration.getId(), "YlDatakbGovernmentServiceIntegration", "id", resources.getId());
        ylDatakbGovernmentServiceIntegration = mapper.map(resources, YlDatakbGovernmentServiceIntegrationModel.class);
        this.saveOrUpdate(ylDatakbGovernmentServiceIntegration);
    }

    @Override
    public void deleteAll(Long[] ids) {
        this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public void download(List<YlDatakbGovernmentServiceIntegrationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YlDatakbGovernmentServiceIntegrationDto ylDatakbGovernmentServiceIntegration : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("长三角专窗数", ylDatakbGovernmentServiceIntegration.getSpecialCount());
            map.put("长三角自助机数", ylDatakbGovernmentServiceIntegration.getSelfHelpCount());
            map.put("长三角事项数", ylDatakbGovernmentServiceIntegration.getMatterCount());
            map.put("网办事项数", ylDatakbGovernmentServiceIntegration.getNetworkCount());
            map.put("数据创建时间", ylDatakbGovernmentServiceIntegration.getCreateTime());
            map.put("数据修改时间", ylDatakbGovernmentServiceIntegration.getUpdateTime());
            map.put("创建人", ylDatakbGovernmentServiceIntegration.getCreatorId());
            map.put("修改人", ylDatakbGovernmentServiceIntegration.getModiflerId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}